{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# ndarray对象属性的基本操作",
   "id": "793464e1776306c2"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 1. ndarray的属性\n",
    "- shape：返回一个元组，表示 array的维度 [形状，几行几列] （2，3）两行三列，（2，2，3）两个两行三列\n",
    "- ndim：返回一个数字，表示array的维度的数目\n",
    "- size：返回一个数字，表示array中所有数据元素的数目\n",
    "- dtype：返回array中元素的数据类型\n"
   ],
   "id": "379e9937a3cc9516"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-25T08:23:24.655489Z",
     "start_time": "2025-08-25T08:23:24.405253Z"
    }
   },
   "cell_type": "code",
   "source": "import numpy as np",
   "id": "c814532986af957",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 1.1 数组的维度",
   "id": "65ded5a1e4f80b4a"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-25T08:26:33.576965Z",
     "start_time": "2025-08-25T08:26:33.566977Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 一维数组\n",
    "arr1 = np.array([1,2,3,4,5])\n",
    "print(arr1,arr1.shape)\n",
    "\n",
    "print('-' * 30)\n",
    "# 二维数组\n",
    "arr2 = np.array([\n",
    "    [1,2,3],\n",
    "    [4,5,6]\n",
    "])\n",
    "\n",
    "print(arr2,arr2.shape)\n",
    "print('-' * 30)\n",
    "\n",
    "arr3 = np.array([\n",
    "    [\n",
    "        [1,2,3],\n",
    "        [4,5,6]\n",
    "    ],\n",
    "    [\n",
    "        [7,8,9],\n",
    "        [10,11,12]\n",
    "    ],\n",
    "    [\n",
    "        [13,14,15],\n",
    "        [16,17,18]\n",
    "    ]\n",
    "])\n",
    "\n",
    "print(arr3,arr3.shape)\n"
   ],
   "id": "2d83b7188d81b20e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5] (5,)\n",
      "------------------------------\n",
      "[[1 2 3]\n",
      " [4 5 6]] (2, 3)\n",
      "------------------------------\n",
      "[[[ 1  2  3]\n",
      "  [ 4  5  6]]\n",
      "\n",
      " [[ 7  8  9]\n",
      "  [10 11 12]]\n",
      "\n",
      " [[13 14 15]\n",
      "  [16 17 18]]] (3, 2, 3)\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### 1.2 ndim 数组的维度\n",
    "### 1.3 size 元素总数"
   ],
   "id": "640bbff286da9694"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-25T08:45:20.944660Z",
     "start_time": "2025-08-25T08:45:20.931471Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 一维数组\n",
    "arr4 = np.array([1,2,3,4,5])\n",
    "print(arr4.shape,arr4.ndim)\n",
    "print(f'一维数组数组arr4的元素总数是：{arr4.size}',f'长度是:{len(arr4)}')\n",
    "\n",
    "print('-' * 30)\n",
    "# 二维数组\n",
    "arr5 = np.array([\n",
    "    [1,2,3],\n",
    "    [4,5,6]\n",
    "])\n",
    "print(arr5.shape,arr5.ndim)\n",
    "print(f'二维数组数组arr的元素总数是：{arr5.size}',f'长度是:{len(arr5)}')\n",
    "\n",
    "print('-' * 30)\n",
    "# 三维数组\n",
    "arr6 = np.array([\n",
    "    [\n",
    "        [1,2,3],\n",
    "        [4,5,6]\n",
    "    ],\n",
    "    [\n",
    "        [7,8,9],\n",
    "        [10,11,12]\n",
    "    ],\n",
    "    [\n",
    "        [13,14,15],\n",
    "        [16,17,18]\n",
    "    ]\n",
    "])\n",
    "print(arr6.shape,arr6.ndim)\n",
    "print(f'三维数组数组arr6的元素总数是：{arr6.size}',f'长度是:{len(arr6)}')"
   ],
   "id": "b025507fb51a2294",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5,) 1\n",
      "一维数组数组arr4的元素总数是：5 长度是:5\n",
      "------------------------------\n",
      "(2, 3) 2\n",
      "二维数组数组arr的元素总数是：6 长度是:2\n",
      "------------------------------\n",
      "(3, 2, 3) 3\n",
      "三维数组数组arr6的元素总数是：18 长度是:3\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 1.4 dtype 元素数组的类型",
   "id": "9cbe430b82c0e2e4"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-25T08:42:25.975077Z",
     "start_time": "2025-08-25T08:42:25.964976Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr7 = np.array([1,2,3,4,5])\n",
    "\n",
    "print(arr7.dtype) # int64  int32  int16  int8\n",
    "# 转换arr7的数据类型\n",
    "b = arr7.astype(float)\n",
    "\n",
    "print(arr7)\n",
    "print(b)\n",
    "print(b.dtype)\n",
    "\n",
    "# 将数组b的元素转换成str类型\n",
    "c = b.astype(str)\n",
    "print(c)\n",
    "print(c.dtype) # <U32 这个表示str类型 <U32 的核心意思是：一个 32位无符号整数（Unsigned 32-bit Integer）\n"
   ],
   "id": "78663d0ccd1ef25c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int64\n",
      "[1 2 3 4 5]\n",
      "[1. 2. 3. 4. 5.]\n",
      "float64\n",
      "['1.0' '2.0' '3.0' '4.0' '5.0']\n",
      "<U32\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### 1.5 数组元素索引(下标)\n",
    "- 数组对象[..., 页号, 行号, 列号]\n",
    "- 下标从0开始，到数组len-1结束。\n"
   ],
   "id": "d6b83c445f2c3b19"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-25T09:07:58.038039Z",
     "start_time": "2025-08-25T09:07:58.027748Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr8 = np.array([3,5,8,9,2])\n",
    "print(arr8[3]) # 9\n",
    "\n",
    "# 二维数组\n",
    "arr9 = np.array([\n",
    "    [3,5,8,9,2],\n",
    "    [2,6,9,4,3]\n",
    "])\n",
    "\n",
    "# 三维数组\n",
    "arr10 = np.array([\n",
    "    [\n",
    "        [1,2,3],\n",
    "        [4,5,6]\n",
    "    ],  # 第一页  arr10[0]\n",
    "    [\n",
    "        [7,8,9],\n",
    "        [10,11,12]\n",
    "    ], # 第二页 arr10[1]\n",
    "    [\n",
    "        [13,14,15],\n",
    "        [16,17,18]\n",
    "    ]  # 第三页 arr10[2]\n",
    "])\n",
    "\n",
    "print(arr9[0]) # [3 5 8 9 2]\n",
    "print(arr9[0][3])\n",
    "print(arr9[0,3])\n",
    "print('='*30)\n",
    "print(arr10[2])  # 第三页\n",
    "print(arr10[2,0]) # 第三页中的第一行\n",
    "print(arr10[2,0,2]) # 第三页中的第一行第三列元素\n",
    "\n",
    "print('='*30)\n",
    "print(arr10.shape)\n",
    "for i in range(arr10.shape[0]): # 遍历页\n",
    "    for j in range(arr10.shape[1]): # 遍历每一行\n",
    "        for k in range(arr10.shape[2]): # 遍历每一列\n",
    "            print(arr10[i,j,k])\n",
    "\n"
   ],
   "id": "a87ee30794fd53db",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n",
      "[3 5 8 9 2]\n",
      "9\n",
      "9\n",
      "==============================\n",
      "[[13 14 15]\n",
      " [16 17 18]]\n",
      "[13 14 15]\n",
      "15\n",
      "==============================\n",
      "(3, 2, 3)\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n",
      "11\n",
      "12\n",
      "13\n",
      "14\n",
      "15\n",
      "16\n",
      "17\n",
      "18\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 1.6 flat 扁平迭代器 它把 “多维”的遍历 变成了 “一维”的遍历。",
   "id": "ebd289c09fbebb4d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-25T09:11:37.364852Z",
     "start_time": "2025-08-25T09:11:37.356790Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# arr10是一个 (3, 2, 3) 形状的三维数组\n",
    "for item in arr10.flat:\n",
    "    print(item)"
   ],
   "id": "9f7aad966ed22492",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n",
      "11\n",
      "12\n",
      "13\n",
      "14\n",
      "15\n",
      "16\n",
      "17\n",
      "18\n"
     ]
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 1.7 T 转置视图，行变列",
   "id": "4666481db6e88aeb"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-25T09:14:02.064958Z",
     "start_time": "2025-08-25T09:14:02.058033Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr11 = np.array([\n",
    "    [1,2,3],\n",
    "    [4,5,6]\n",
    "])\n",
    "print(arr11,arr11.shape)\n",
    "\n",
    "print('='*30)\n",
    "\n",
    "arr12 = arr11.T\n",
    "print(arr12,arr12.shape)"
   ],
   "id": "9693910972d357f3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]] (2, 3)\n",
      "==============================\n",
      "[[1 4]\n",
      " [2 5]\n",
      " [3 6]] (3, 2)\n"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### 1.8 itemsize 每个元素字节数\n",
    "### 1.9 nbytes 总字节数(size * itemsize)"
   ],
   "id": "10be19db02c11167"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-25T09:16:36.571953Z",
     "start_time": "2025-08-25T09:16:36.561457Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr13 = np.array([1,2,3,4,5])\n",
    "\n",
    "print(arr13.dtype,arr13.itemsize)\n",
    "print(arr13.nbytes)"
   ],
   "id": "8d89db0932a6a3d9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int64 8\n",
      "40\n"
     ]
    }
   ],
   "execution_count": 38
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
